﻿namespace IBTrader.Indicator.Oscillator
{
    using IBTrader.Indicator.Average;
    using IBTrader.Modules.Read;
    using IBTrader.Charts;
    using System.Linq;

    /// <summary>
    /// The Percentage Volume Oscillator (PVO) is a momentum oscillator for volume. 
    /// PVO measures the difference between two volume-based moving averages as a percentage of the larger moving average. 
    /// As with MACD and the Percentage Price Oscillator (PPO), it is shown with a signal line, a histogram and a centerline. 
    /// PVO is positive when the shorter volume EMA is above the longer volume EMA and negative when the shorter volume EMA is below. 
    /// This indicator can be used to define the ups and downs for volume, which can then be use to confirm or refute other signals. 
    /// Typically, a breakout or support break is validated when PVO is rising or positive.
    /// </summary>
    class PVO : Base, IVolume
    {
        private EMA[] emas;
        public PVO(Worker worker, int interval1 = 12, int interval2 = 26, int interval3 = 9) : base(worker, new ChartArgs { Group = 1 })
        {
            Line line1 = null;
            emas = new[] { interval1, interval2, interval3 }.Select(i => new EMA(null, i)).ToArray();
            emas[0].Hit += (_, line) => line1 = line;
            emas[1].Hit += (sender, line) => emas[2].Add(sender, NewLine(line, NaN((line1.Price - line.Price) / line.Price) * 100));
            emas[2].Hit += (_, line) => OnHit(line, line.Price);
        }
        public override void Add(object _, Line line)
        {
            // only volume used
        }
        public void AddVolume(object sender, Line line)
        {
            emas[0].Add(sender, NewLine(line, line.Price));
            emas[1].Add(sender, NewLine(line, line.Price));
        }
    }
}